package com.yunhe.transfer.task;

import com.yunhe.common.model.authority.Station;
import com.yunhe.transfer.client.AuthorityClient;
import com.yunhe.common.model.authority.StationDTO;
import com.yunhe.transfer.mqtt.HeartbeatMqttHandler;
import com.yunhe.transfer.mqtt.MeasurementMqttHandler;
import com.yunhe.transfer.mqtt.RunStrategyMqttHandler;
import net.javacrumbs.shedlock.spring.annotation.SchedulerLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.*;

/**
 * 定时更新电站信息任务
 * @author duanxiaoyong
 * @desc 每15s更新一次电站信息，更新新增电站的策略、心跳、数据上传、告警上传。
 */
@EnableScheduling
@Configuration
public class StationUpdateTask {

    private List<Station> stations;
    @Autowired
    private AuthorityClient authorityClient;
    @Autowired
    private RunStrategyMqttHandler runStrategyMqttHandler;
    @Autowired
    private MeasurementMqttHandler measurementMqttHandler;
    @Autowired
    private HeartbeatMqttHandler heartbeatMqttHandler;

    @SchedulerLock(name = "StationSubscribeTask", lockAtMostFor = "PT10S")
    @Scheduled(cron = "0/15 * * * * ?")
    public void task() {
        //获取缓存中的电站信息
        List<Long> stationCodes = new ArrayList<>();
        List<String> beeIds = new ArrayList<>();
        getStationCodesAndBeeIds(stations, stationCodes, beeIds);
        //获取全部电站信息
        List<Station> stationList = authorityClient.getStations(null).getResults();
        List<Long> stationCodeList = new ArrayList<>();
        List<String> beeIdList = new ArrayList<>();
        Map<String, Station> beeIdStationMap = getStationCodesAndBeeIds(stationList, stationCodeList, beeIdList);
        //判断是否有新增的电站信息
        stationCodeList.removeAll(stationCodes);
        beeIdList.removeAll(beeIds);
        if (stationCodeList.size() > 0) {
            stations = stationList;
            stationCodeList.forEach(stationCode -> {
                runStrategyMqttHandler.subscribe(stationCode);
                measurementMqttHandler.subscribeCollect(stationCode);
                measurementMqttHandler.subscribeOrderCollect(stationCode);
                heartbeatMqttHandler.subscribe(stationCode);
            });
        }
        if (beeIdList.size() > 0) {
            beeIdList.forEach(beeId -> {
                Station station = beeIdStationMap.get(beeId);
                if (station != null) {
                    measurementMqttHandler.subscribeDpuData(station.getId(), station.getTimeZone(), beeId);
                }
            });
        }
    }

    private Map<String, Station> getStationCodesAndBeeIds(List<Station> stations, List<Long> stationCodes, List<String> beeIds) {
        Map<String, Station> beeIdStationMap = new HashMap<>();
        if (stations != null) {
            stations.forEach(station -> {
                if (station.getId() != null) {
                    stationCodes.add(station.getId());
                }
                if (station.getBeeIds() != null) {
                    List<String> stationBeeIds = station.getBeeIds();
                    beeIds.addAll(stationBeeIds);
                    for (String beeId : stationBeeIds) {
                        beeIdStationMap.put(beeId, station);
                    }
                }
            });
        }
        return beeIdStationMap;
    }
}
